package org.zjt.platform.demo.config;

import org.zjt.platform.demo.config.properties.RedisSentinelProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author <a href="juntao.zhang@qq.com">juntao.zhang</a>
 * @Description:
 * @Package org.zjt.platform.traceworker.config
 * @date 2018/1/12 11:27
 * @see
 */
@Configuration
@EnableConfigurationProperties(RedisSentinelProperties.class)
@ConditionalOnProperty(prefix="sentinel.redis",name = "enable", havingValue = "true")
public class SentinelRedisConfiguration {

    @Bean
    public RedisSentinelConfiguration redisSentinelConfiguration(RedisSentinelProperties redisSentinelProperties) {
        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
        RedisNode masterNode = new RedisNode.RedisNodeBuilder()
                .withName(redisSentinelProperties.getMasterNode()).build();
        List<RedisNode> sentinels = redisSentinelProperties.getSentinels().stream()
                .map(item -> new RedisNode.RedisNodeBuilder().listeningAt(item.getHost(), item.getPort()))
                .map(RedisNode.RedisNodeBuilder::build)
                .collect(Collectors.toList());
        redisSentinelConfiguration.setMaster(masterNode);
        redisSentinelConfiguration.setSentinels(sentinels);
        return redisSentinelConfiguration;
    }


    @Bean
    public JedisPoolConfig jedisPoolConfig(RedisSentinelProperties redisSentinelProperties) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(redisSentinelProperties.getMxIdle());
        jedisPoolConfig.setMaxWaitMillis(redisSentinelProperties.getMaxWaitMillis());
        jedisPoolConfig.setTestOnBorrow(redisSentinelProperties.getTestOnBorrow());
        jedisPoolConfig.setMaxTotal(redisSentinelProperties.getMaxTotal());
        return jedisPoolConfig;
    }


    @Bean
    public JedisConnectionFactory jedisConnectionFactory(RedisSentinelProperties redisSentinelProperties) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisSentinelConfiguration(redisSentinelProperties),jedisPoolConfig(redisSentinelProperties));
        return jedisConnectionFactory;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(jedisConnectionFactory);
        return stringRedisTemplate;
    }


}
